package com.zn.net.socket
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class SocketPackageIn extends ByteArray
	{
		private var _len:int;
		
		private var _checksum:int;
		
		private var _clientId:int;
		
		private var _code:int;
		
		private var _extend1:int;
		
		private var _extend2:int;
		
		private var _body:ByteArray;
		
		public function SocketPackageIn()
		{
			super();
			
			_body=new ByteArray();
			_body.endian =endian= Endian.BIG_ENDIAN;
		}
		
		public function load(src:ByteArray,len:int):void
		{
			src.readBytes(this,0,len);
			readHeader();
			
			readBytes(_body,0,bytesAvailable);
		}
		
		public function readHeader():void
		{
			_len = readShort();
			_checksum = readShort();
			_code = readShort();
			_clientId = readInt();
			_extend1 = readInt();
			_extend2 = readInt();
		}
		
		
		public function get checkSum():int
		{
			return _checksum;
		}
		
		public function get code():int
		{
			return _code;
		}
		
		public function get clientId():int
		{
			return _clientId;
		}
		
		public function get extend1():int
		{
			return _extend1;
		}
		
		public function get extend2():int
		{
			return _extend2;
		}
		
		public function get Len():int
		{
			return _len;
		}
		
		public function get body():ByteArray
		{
			return _body;
		}
		
		public function calculateCheckSum():int
		{
			var val1:int = 0x77;
			var i:int = 6;
			var len:int = this.length;
			
			while (i < len)
			{
				val1 += this[i++];
			}
			
			return (val1) & 0x7F7F;
		}
		
		public function get isCheckPass():Boolean
		{
			return checkSum==calculateCheckSum();
		}
	}
}